/**
 * 签章插件
 * Created by suweiming on 2018/1/13.
 */
(function ($) {
    var $signature = {
        genChaperId: function (tag) {
            return (tag || 'signature') + formatDate(new Date()) + parseInt(Math.random() * 1000);
        },
        init: function (options) {
            var self = this;
            onClick($(this), function () {//
                if (!$('#signature-table-list').length) {
                    var $signatures = $(ejs.render($('#signature-list').html(), {list: options.chapters}));
                    $signatures.appendTo($('body'));
                    $signatures.show();
                    $signatures.draggabilly({});
                    onClick($signatures.find('tbody tr'), function () {
                        $(this).addClass('active');
                        $(this).siblings('tr').removeClass('active');
                        $(options.eleId).addClass('signature_');
                    });

                    onClick($(options.eleId), function (e) {
                        e.stopPropagation(); //
                        if ($('.item.fl.flag').length) {
                            return;
                        }
                        if (!$signatures.find('.active img').length) {
                            return layer.msg('请选择所用签章！');
                        }
                        var $img = $('<img/> ').attr('src', $signatures.find('.active img').attr('src')).css({'width': '100%'});
                        var $chapter = $('<div/>').addClass($signature.genChaperId() + ' chapter-signature').css({
                            position: 'absolute',
                            width: '200px',
                            height: '200px',
                            left: e.pageX - $(this).offset().left - 100,
                            top: e.pageY - $(this).offset().top - 100,
                        });
                        var ok = $('<span class="item fl flag"><i class="fa fa-check"></i></span>');
                        var cancel = $('<span class="item fl"><i class="fa fa-times"></i></span>');
                        var handler = $('<div class="chapter-handler"></div>');
                        $chapter.append($img).append(handler.append(ok).append(cancel));
                        onClick($chapter, function (e) {
                            e.stopPropagation();

                        })
                        $(this).append($chapter);
                        $chapter.draggabilly();
                        onClick(cancel, function (e) {
                            e.stopPropagation();
                            $(this).closest('div.chapter-handler').parent().remove();
                        });
                        onClick(ok, function (e) {
                            e.stopPropagation();
                            if (confirm('确定签章？')) {
                                $(options.eleId).removeClass('signature_');
                                $chapter.draggabilly('disable');// 禁用移动
                                $(this).closest('div').remove();
                                options.ok && options.ok();
                            }
                        });
                    })
                }
            })
        },
        initBtn: function () {
        },

        $change: function (options) {
            var self = this;
            onClick($(this), function () {//
                // if (!$('#signature-table-list').length) { --- feature
                // var $signatures = $(ejs.render($('#signature-list').html(), {list: options.chapters}));
                // $signatures.appendTo($('body'));
                // $signatures.show();
                // $signatures.draggabilly({});
                // onClick($signatures.find('tbody tr'), function () {
                //     $(this).addClass('active');
                //     $(this).siblings('tr').removeClass('active');
                //     $(options.eleId).addClass('signature_');
                // });

                onClick($(options.eleId), function (e) {
                    e.stopPropagation(); //
                    if ($('.item.fl.flag').length) {
                        return;
                    }
                    var $input = $('<textarea></textarea>').css({'width': '100%',height:'100'});
                    var $text = $('<pre/>');
                    $text.text('请输入变更内容');
                    $input.attr('placeholder','请输入变更内容');
                    $text.css({'backgroundColor':'transparent','border':'none',padding:0,margin:0,'font-size':'18pt'})

                    var $chapter = $('<div/>').addClass($signature.genChaperId('change') + ' change-signature').css({
                        position: 'absolute',
                        minWidth: '200px',
                        height: '100px',
                        left: e.pageX - $(this).offset().left - 100,
                        top: e.pageY - $(this).offset().top - 100,
                    });
                    var ok = $('<span class="item fl flag"><i class="fa fa-check"></i></span>');
                    var cancel = $('<span class="item fl"><i class="fa fa-times"></i></span>');
                    var handler = $('<div class="chapter-handler"></div>');
                    handler.css({'top':'-30px'});
                    $chapter.append($text).append($input).append(handler.append(ok).append(cancel));
                    onClick($chapter, function (e) {
                        e.stopPropagation();
                    });
                    onInput($input,function(){
                        $text.html($input.val());
                    });
                    $(this).append($chapter);
                    $chapter.draggabilly();
                    onClick(cancel, function (e) {
                        e.stopPropagation();
                        $(this).closest('div.chapter-handler').parent().remove();
                    });
                    onClick(ok, function (e) {
                        e.stopPropagation();
                        if (confirm('确定变更？')) {
                            ///$(options.eleId).removeClass('signature_');
                            $chapter.draggabilly('disable');// 禁用移动
                            $(this).closest('.change-signature').find('textarea').remove();
                            $(this).closest('div').remove();
                            options.ok && options.ok();
                        }
                    });
                })
                // }
            })
        },
    };
    /**
     *  签章
     * @param options
     * @returns {*}
     */
    $.fn.$signature = function (options) {
        if (!(options.chapters instanceof Array && options.chapters.length > 0)) {
            return Log("签章参数错误！")
        }
        var _default = {
            chapters: [], //签章数组
            eleId: '', //目标对象#ID
        };
        options = $.extend({}, _default, options || {});
        //$(this).html('<div>加载中...</div>');
        $(options.eleId).removeClass('signature_');
        return this.each(function () {
            $signature.init.call(this, options);
        })
    };
    $.fn.$change = function (options) {
        // if (!(options.chapters instanceof Array && options.chapters.length > 0)) {
        //     return Log("签章参数错误！")
        // }
        var _default = {
            chapters: [], //签章数组
            eleId: '', //目标对象#ID
        };
        options = $.extend({}, _default, options || {});
        //$(this).html('<div>加载中...</div>');
        $(options.eleId).removeClass('signature_');
        return this.each(function () {
            $signature.$change.call(this, options);
        })
    };
})(jQuery);


